

#ifndef DRAW_UTILS_H
#define DRAW_UTILS_H

#include <Vec4.h>


#include <OpenEXR/ImathVec.h>
#include <OpenEXR/ImathBox.h>

#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>


class DrawUtils
{
	
	public:
		
		// Draw an axis aligned, origin centered box with the given half-extents.
		static void drawBox(Vec4 halfExtents);
		
		// Draw a quad with specified vertices and normal.
		static void drawQuad(const Vec4& v1, const Vec4& v2, const Vec4& v3, const Vec4& v4, const Vec4& n);
		
		// Draw crosshairs with specified position and size.
		static void drawCrosshairs(float size, const Vec4& P);
	
		
	// This would be user implemented.
	// All calls to debug draw functions defined out.
	static inline void drawLine(Imath::V3f start, Imath::V3f end, float r, float g, float b, float a=1.0f)
	{
		glColor4f(r, g, b, a);
		glBegin(GL_LINES);
		glVertex3fv(&start[0]);
		glVertex3fv(&end[0]);
		glEnd();
	}
	
	static inline void drawTriangle(Imath::V3f A, Imath::V3f B, Imath::V3f C, float r, float g, float b, float a=1.0f)
	{
		drawLine(A, B, r, g, b, a);
		drawLine(B, C, r, g, b, a);
		drawLine(C, A, r, g, b, a);
	}
	
	static inline void drawQuad(const Imath::V3f& v1, const Imath::V3f& v2, const Imath::V3f& v3, const Imath::V3f& v4, float r, float g, float b, float a=1.0f)
	{
		drawLine(v1, v2, r, g, b, a);
		drawLine(v2, v3, r, g, b, a);
		drawLine(v3, v4, r, g, b, a);
		drawLine(v4, v1, r, g, b, a);
	}
	
	static inline void drawBox(Imath::Box3f box, float r, float g, float b, float a=1.0f)
	{
		drawBox(0.5f*(box.min+box.max), 0.5f*(box.min-box.max), r, g, b, a);
	}
	
	static inline void drawBox(Imath::V3f center, Imath::V3f halfExtents, float r, float g, float b, float a=1.0f)
	{
		glPushMatrix();
		glTranslatef(center[0], center[1], center[2]);
		Imath::V3f v[8];
		unsigned int i,j;
		for (i = 0; i < 8; ++i) 
			for (j = 0; j < 3; ++j) 
				v[i][j] = halfExtents[j] * ( -1.0f + 2.0f*(float) ( (i >> j) & 1 ) );
		drawQuad(v[0],v[2],v[3],v[1],r,g,b,a);
		drawQuad(v[5],v[7],v[6],v[4],r,g,b,a);
		drawQuad(v[0],v[1],v[5],v[4],r,g,b,a);
		drawQuad(v[2],v[6],v[7],v[3],r,g,b,a);
		drawQuad(v[0],v[4],v[6],v[2],r,g,b,a);
		drawQuad(v[3],v[7],v[5],v[1],r,g,b,a);
		glPopMatrix();
	}
	
	static inline void drawCrosshairs(float size, const Imath::V3f& P, float r, float g, float b, float a=1.0f)
	{
		Imath::V3f P0(P); Imath::V3f P1(P); 
		Imath::V3f P2(P); Imath::V3f P3(P); 
		Imath::V3f P4(P); Imath::V3f P5(P);
		P0[0] += size; P1[0] -= size; 
		P2[1] += size; P3[1] -= size;
		P4[2] += size; P5[2] -= size;
		drawLine(P0, P1, r, g, b, a);
		drawLine(P2, P3, r, g, b, a);
		drawLine(P4, P5, r, g, b, a);
	}
	
	
};






#endif //DRAW_UTILS_H
